import { defineStore } from 'pinia';
interface TagRaw {
  path: string;
  name: string;
  closed?: boolean;
}
export const useAppStore = defineStore('app', {
  state: () => {
    return {
      //主题
      theme: 'dark',
      //主题颜色
      primaryColor: '#409eff',
      //是否展示页签
      showTabs: true,
      //页签列表
      tabsList: [
        {
          path: '/workbench',
          name: '首页/工作台',
          closed: false,
        },
        {
          path: '/hellow',
          name: '首页/欢迎',
        },
      ],
      //当前页签
      activeTab: '/workbench',
    } as any;
  },
  getters: {
    getTheme(): string {
      return this.theme;
    },
    getPrimaryColor(): string {
      return this.primaryColor;
    },
    getShowTabs(): boolean {
      return this.showTabs;
    },
    getTabsList(): TagRaw[] {
      return this.tabsList;
    },
    getActiveTab(): string {
      return this.activeTab;
    },
  },
  actions: {
    //设置主题
    setTheme(theme: string) {
      this.theme = theme;
    },
    //设置主题颜色
    setPrimaryColor(color: string) {
      this.primaryColor = color;
    },
    //设置是否展示页签
    setShowTabs(showTabs: boolean) {
      this.showTabs = showTabs;
    },
    //添加页签
    addTab(tab: TagRaw) {
      this.tabsList.push(tab);
    },
    //删除页签
    removeTab(tab: TagRaw) {
      const index = this.tabsList.findIndex(
        (item: TagRaw) => item.path === tab.path
      );
      //如果关闭的页签是当前页签，则切换到前一个页签
      if (index === this.tabsList.length - 1) {
        this.activeTab = this.tabsList[index - 1].path;
      }
      //移除
      this.tabsList.splice(index, 1);
    },
  },
});
